package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.PeachBlossom;
import com.tanhua.domain.vo.PeachBlossomVO;
import com.tanhua.dubbo.api.PeachBlossomApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;
import java.util.Random;

/**
 * @author anshuai
 * @create 2021-02-28 20:12
 */
@Service
public class PeachblossomService {

    @Reference
    private PeachBlossomApi peachBlossomApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 桃花传音-发送语音
     *
     * @param soundFile 客户端传过来的语音文件
     * @return
     */
    public ResponseEntity sendVoice(MultipartFile soundFile) throws IOException {
        //1.把语音存储到fastdfs
        //获取音频文件原始文件名
        String originalFilename = soundFile.getOriginalFilename();
        //获取文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        //上传到fastdfs服务器上需要三个参数:1.文件输入流、2.文件大小、3.文件后缀名
        StorePath uploadFile = fastFileStorageClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(), suffix, null);
        //获取文件路径
        String fullPath = uploadFile.getFullPath();
        //拼接文件路径
        String soundPath = fdfsWebServer.getWebServerUrl() + fullPath;
        System.out.println("文件存储到:" + soundPath);
        //3.把音频文件信息存储到MongoDB中
        PeachBlossom peachBlossom = new PeachBlossom();
        peachBlossom.setUserId(UserHolder.getUserId());
        peachBlossom.setSoundUrl(soundPath);
        peachBlossom.setCreated(System.currentTimeMillis());

        peachBlossomApi.saveSound(peachBlossom);

        //保存当日接收次数
        redisTemplate.opsForValue().set("RemainingTimes_" + UserHolder.getUserId(), "10", Duration.ofDays(1));

        return ResponseEntity.ok(null);
    }

    /**
     * 桃花传音-接收语音
     *
     * @return
     */
    public ResponseEntity receivingVoice() {
        //1.从MongoDB中随机查询一个音频信息
        long userId = new Random().nextInt(10) + 1;
        PeachBlossom peachBlossom = peachBlossomApi.findVoice(userId);

        //2.调用Api查询用户信息
        UserInfo userInfo = userInfoApi.findById(userId);

        //3.从redis中获取今日剩余次数
        String count = redisTemplate.opsForValue().get("RemainingTimes_" + UserHolder.getUserId());
        //封装返回数据
        PeachBlossomVO vo = new PeachBlossomVO();
        vo.setId(UserHolder.getUserId());
        vo.setAge(userInfo.getAge());
        vo.setAvatar(userInfo.getAvatar());
        vo.setGender(userInfo.getGender());
        vo.setNickname(userInfo.getNickname());
        vo.setSoundUrl(peachBlossom.getSoundUrl());
        vo.setRemainingTimes(Integer.parseInt(count));

        //将今日查询次数减一
        redisTemplate.opsForValue().decrement("RemainingTimes_" + UserHolder.getUserId());
        //返回数据
        return ResponseEntity.ok(vo);
    }
}
